package com.hb.common.handler.backup;

import cn.hutool.core.util.StrUtil;
import com.sl.ecm.backup.constant.DbCommand;
import com.sl.ecm.backup.dto.Database;
import com.sl.ecm.backup.enums.DbEnum;
import com.sl.ecm.backup.factory.DbBackupFactory;
import com.sl.ecm.backup.handler.os.OSHandler;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * SqlServer备份处理程序
 */
@Component
@Lazy(false)
public class SqlServerBackupHandler extends AbstractDbBackupHandler {

	@Override
	protected void backupAfter(String dbBackupPath) {

	}

	@Override
	protected void restoreAfter(String dbProgramPath) {

	}

    /**
     * 创建SqlServer备份命令
     *
     * @param database   数据源信息
     * @param dbFilePath 备份文件路径
     * @return 备份命令字符串
     */
    @Override
    protected String[] createBackupCommand(Database database, String dbFilePath) {
        String command = StrUtil.format(DbCommand.SQLSERVER_BACKUP,
                database.getHost(),
                database.getPort(),
                database.getUsername(),
                database.getPassword(),
                database.getDbName(),
                dbFilePath);
        // 组装命令
        OSHandler osHandler = osUtil.getOSHandler();
        return osHandler.assembleCommand(command);
    }

    /**
     * 创建SqlServer还原命令
     *
     * @param database   数据源信息
     * @param dbFilePath 还原文件路径
     * @return 还原命令字符串
     */
    @Override
    protected String[] createRestoreCommand(Database database, String dbFilePath) {
        String command = StrUtil.format(DbCommand.SQLSERVER_RESTORE,
                database.getHost(),
                database.getPort(),
                database.getUsername(),
                database.getPassword(),
                database.getDbName(),
                dbFilePath);
        // 组装命令
        OSHandler osHandler = osUtil.getOSHandler();
        return osHandler.assembleCommand(command);
    }

    /**
     * 获取SqlServer数据库程序路径
     *
     * @return SqlServer数据库程序路径
     */
    @Override
    protected String dbProgramPath() {
        OSHandler osHandler = osUtil.getOSHandler();
        return pathPermissions.getDbProgramPath() + osHandler.getOsPath() + DbEnum.SQL_SERVER.name().toLowerCase() + "/bin";
    }

    /**
     * 注册SqlServer备份处理程序
     */
    @Override
    public void afterPropertiesSet() {
        DbBackupFactory.register(DbEnum.SQL_SERVER, this);
    }

    @Override
    protected String dbFileTye() {
        return "sql";
    }
}
